<template>
  <div style="margin: auto;margin-left: 200px; margin-right: 200px;">
    <h1 style="text-align: center;">账号管理</h1>
    <div style="margin-top: 15px;">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="用户id" width="180">
        </el-table-column>
        <el-table-column prop="username" label="用户名" width="180">
        </el-table-column>
        <el-table-column prop="password" label="密码">
        </el-table-column>
        <el-table-column prop="email" label="邮箱">
        </el-table-column>
        <el-table-column prop="gmtCreate" label="添加日期">
        </el-table-column>
        <el-table-column prop="gmtModified" label="修改日期">
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <router-link :to="'/user/update/'+scope.row.id">
              <el-button type="primary" size="small">编辑</el-button>
            </router-link>
            <el-button @click="deleteUserById(scope.row.id,1)" v-if="scope.row.isDeleted == 0" type="danger"
              size="small">封禁</el-button>
            <el-button @click="deleteUserById(scope.row.id,0)" v-if="scope.row.isDeleted == 1" type="success"
              size="small">解封</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination :current-page="page" :page-size="limit" :total="total" style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper" @current-change="getAllUsera" />
  </div>
</template>

<script>
  import user from '@/api/user'
  export default {
    data() {
      return {
        page: 1, //当前页
        limit: 6, //每页记录数
        total: 0, //总记录数
        tableData: [{
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        }, {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        }, {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }]
      }
    },
    created() {
      this.getAllUsera()
    },
    methods: {
      getAllUsera(page = 1) {
        this.page = page
        user.getAllUser(this.page, this.limit)
          .then(response => {

            this.tableData = response.data.user.records
            this.page = response.data.user.current
            this.total = response.data.user.total
            console.log(this.tableData)
          })
      },
      deleteUserById(id, num) {
        if (num == 1) {
          this.$confirm('此操作将封禁该账号, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            user.deleteUser(id,num)
              .then(response => {
                //提示信息
                this.$message({
                  type: 'success',
                  message: '封禁成功!'
                });
                //回到列表页面
                this.getAllUsera()
              })
          }) //点击取消，执行catch方法
        }else{
          this.$confirm('此操作将激活该账号, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            user.deleteUser(id,num)
              .then(response => {
                //提示信息
                this.$message({
                  type: 'success',
                  message: '激活成功!'
                });
                //回到列表页面
                this.getAllUsera()
              })
          }) //点击取消，执行catch方法
        }
      }
    }
  }
</script>
